﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace CodeStatistic
{
    public partial class Form_CodeStatistic : Form
    {
       
        List<CodeFile> codeFiles = new List<CodeFile>();
        int codeFileCount = 0;
        int progressCount = 0;
        bool folderSelected = false;
        
        long fileTotalLength=0;
        string folderPath;
        StringBuilder sb=new StringBuilder();
        public Form_CodeStatistic()
        {
            InitializeComponent();
        }

        private void CodeStatistic()
        {
            
            ResetStatistic();
            DirectoryInfo directoryInfo= new DirectoryInfo(folderPath);
            TravelAllFolder(directoryInfo);
            codeFileCount=codeFiles.Count;
            if (codeFileCount == 0)
            {
                richTextBox1.Text = "没有代码文件";
            }
            else
            {
                timer_StatisticNum.Start();
                foreach (var a in codeFiles)
                {
                    long fileSize = a.fileSize;
                    fileTotalLength += fileSize;
                    progressCount++;
                    sb.AppendLine("["+progressCount+"]:"+ a.file + "    \t大小:" + fileSize + "Byte=" + (Convert.ToDouble(fileSize) / 1024).ToString("F2") + "KB");
                }
                richTextBox1.Text= "统计时间:" + DateTime.Now.ToString("g")+"\n";
                richTextBox1.Text+= "路径:[" + folderPath + "]下总共有有效代码文件:[" + codeFileCount + "]个\n";
                richTextBox1.Text += "有效代码文件总大小:" + fileTotalLength + "Byte=" + (Convert.ToDouble(fileTotalLength) / 1024).ToString("F2") +
                    "KB=" + (Convert.ToDouble(fileTotalLength) / (1024 * 1024)).ToString("F2") + "MB\n";

                richTextBox1.Text+=sb.ToString();
                button_SaveResult.Enabled = true;
                timer_StatisticNum.Stop();
                SetStatictisProgress(true);
            }

        }
        private void TravelAllFile(DirectoryInfo parentDirectory)
        {
            var files = parentDirectory.GetFiles();
            foreach (var file in files)
            {
                if (file.Extension == ".cs")
                {
                    string fileName = file.Name;
                    if (!CodeFile.CheckIsExceptionFile(fileName))
                    {
                        codeFiles.Add(new CodeFile(file.FullName, file.Length));
                    }
                }
            }
        }
        private void TravelAllFolder(DirectoryInfo parentDirectory)
        {
            var directories = parentDirectory.GetDirectories();
            if(directories.Count()>0)
            {
                foreach(var directory in directories)
                {
                    TravelAllFolder(directory);
                }
                TravelAllFile(parentDirectory);
            }
            else
            {
                TravelAllFile(parentDirectory);
            }
        }
        private void button_StartStatistic_Click(object sender, EventArgs e)
        {
            if(!folderSelected)
            {
                if (SelectFolder())
                {
                    CodeStatistic();
                }
                
            }
            else
            {
                CodeStatistic();
            }
        }
        private void ResetStatistic()
        {
            codeFiles = new List<CodeFile>();
            button_SaveResult.Enabled = false;
            label_StatisticNum.Text = "[0/0]-100%";
            progressBar_StatictisProgress.Value = 0;
            codeFileCount = 0;
            progressCount = 0;
        }
        private bool SelectFolder()
        { 
            var result = folderBrowserDialog_SelectFolder.ShowDialog();
            if (result==DialogResult.OK)
            {
                ResetStatistic();
                folderPath =folderBrowserDialog_SelectFolder.SelectedPath;
                richTextBox1.Text= "已经选择文件路径:["+folderPath+"]";
                folderSelected = true;
                return true;
            }
            return false;
        }
        private void button_ChooseFolder_Click(object sender, EventArgs e)
        {
            SelectFolder();
        }
        private void SetStatictisProgress(bool IsDone=false)
        {
            if(IsDone)
            {
                label_StatisticNum.Text="["+codeFileCount+"/"+codeFileCount+"]-100%";
                progressBar_StatictisProgress.Value = 100;
            }
            else
            {
                label_StatisticNum.Text = "[" + progressCount + "/" + codeFileCount + "]-"+(progressCount/codeFileCount).ToString("P2");
                progressBar_StatictisProgress.Value = progressCount*100 / codeFileCount;
            }
        }

        private void button_SaveResult_Click(object sender, EventArgs e)
        {
            var result = saveFileDialog_CodeStatistic.ShowDialog();
            if (result == DialogResult.OK)
            {
                FileStream fs=new FileStream(saveFileDialog_CodeStatistic.FileName,FileMode.Create);
                StreamWriter sw=new StreamWriter(fs);
                sw.Write(richTextBox1.Text);
                sw.Flush();
                sw.Close();
                fs.Close();
            }

        }

        private void timer_StatisticNum_Tick(object sender, EventArgs e)
        {
            SetStatictisProgress();
        }
    }
}
